library SummonChestAbility initializer Init requires entities, utils
    /*
    --------------------------------------------------------------------------
    # 召唤宝箱
    --------------------------------------------------------------------------
    */
    globals
        public integer ABILITY_ID = 'AC05'
    endglobals

    struct SummonChest extends array

        unit m_chest
        real m_x
        real m_y

        static method get takes unit u returns thistype
            return thistype(Unit.get(u))
        endmethod

        method setChest takes unit chest returns nothing
            set this.m_chest = chest
            set this.m_x = GetUnitX(chest)
            set this.m_y = GetUnitY(chest)
        endmethod

    endstruct

    private function SpellEnd takes Spell spell returns nothing
        local SummonChest seu = SummonChest.get(spell.m_unit)

        set spell.m_path = "Abilities\\Spells\\Human\\MarkOfChaos\\MarkOfChaosDone.mdl"
        call AddEffect(spell.m_path, GetUnitX(seu.m_chest), GetUnitY(seu.m_chest))

        call AddEffect("Abilities\\Spells\\Orc\\FeralSpirit\\feralspiritdone.mdl", seu.m_x, seu.m_y)
        call SetUnitXY(seu.m_chest, seu.m_x, seu.m_y)
        call IssueImmediateOrderById(seu.m_chest, ORDER_NOTARG_STOP)
        call SetUnitFacing(seu.m_chest, bj_UNIT_FACING)

        set spell.m_boolean = false
        call spell.destroy()
    endfunction

    private function SpellAction takes Spell spell returns nothing
    endfunction

    private function anno_3df1d983 takes nothing returns nothing
        local Spell spell = GetTimerData(GetExpiredTimer())
        local SummonChest seu = SummonChest.get(spell.m_unit)
        local real distance = GetDistanceByUnitXY(spell.m_unit, seu.m_chest)

        if distance > 400.00 then
            call SpellEnd(spell)
        endif
    endfunction

    private function anno_69a4523f takes nothing returns nothing
        local Spell spell = GetTimerData(GetExpiredTimer())
        local SummonChest seu = SummonChest.get(spell.m_unit)

        set spell.m_path = "Abilities\\Spells\\Human\\MarkOfChaos\\MarkOfChaosDone.mdl"
        call AddEffect(spell.m_path, GetUnitX(seu.m_chest), GetUnitY(seu.m_chest))

        call SetUnitXY(seu.m_chest, spell.m_targetX, spell.m_targetY)
        call IssueImmediateOrderById(seu.m_chest, ORDER_NOTARG_STOP)

        call TimerStart(spell.getTimer(), 0.5, true, function anno_3df1d983)
    endfunction

    private function SpellEffect takes nothing returns nothing
        local Spell spell

        if not (IsWidgetAlive(SpellEvent.CastingUnit)) then
            return
        endif

        set spell = Spell.create(SpellEvent.CastingUnit, ABILITY_ID)

        set spell.m_angle = GetUnitFacing(spell.m_unit)
        set spell.m_targetX = GetUnitX(spell.m_unit) + GetDistanceX(120.0, spell.m_angle)
        set spell.m_targetY = GetUnitY(spell.m_unit) + GetDistanceY(120.0, spell.m_angle)

        // 用于判断是否还在上一次施放技能中
        set spell.m_boolean = true

        set spell.m_path = "Abilities\\Spells\\Orc\\FeralSpirit\\feralspiritdone.mdl"
        call AddEffect(spell.m_path, spell.m_targetX, spell.m_targetY)

        call TimerStart(spell.getTimer(), 0.4, false, function anno_69a4523f)
    endfunction

    private function SpellChannel takes nothing returns nothing
        local Spell spell = Spell.create(SpellEvent.CastingUnit, ABILITY_ID)

        // 判断是否还在上一次施放技能中
        if spell.m_boolean then
            call IssueImmediateOrderById(spell.m_unit, ORDER_NOTARG_STOP)
            call SimError(GetOwningPlayer(spell.m_unit), "不能在此位置施放")
        endif
    endfunction

    private function Init takes nothing returns nothing
        call RegisterSpellChannelResponse(ABILITY_ID, SpellChannel)
        call RegisterSpellEffectResponse(ABILITY_ID, SpellEffect)
    endfunction

endlibrary
